<?php
class km_barang_supplier_model extends MY_Model {
	var $model = 'km_barang_supplier';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	bs_id, a.supplier_id, a.barang_id, bs_date_begin, bs_date_end, bs_nilai, bs_status, bs_return,supplier_nama,barang_nama
					FROM	km_barang_supplier a JOIN km_supplier b on  a.supplier_id=b.supplier_id JOIN km_barang c on a.barang_id=c.barang_id
					WHERE	(UPPER(supplier_nama) LIKE '%".strtoupper(post('keyword'))."%' OR 
							UPPER(barang_nama) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'barang_nama' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('km_barang_supplier', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('km_barang_supplier', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_roleid){
		$result = $this->db->get_where('km_barang_supplier', array('bs_id' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_roleid)
	{
		$result = $this->db->get_where('km_barang_supplier', array('bs_id' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM km_barang_supplier km_barang_supplier a JOIN km_supplier b on  a.supplier_id=b.supplier_id JOIN km_barang c on a.barang_id=c.barang_id WHERE a.bs_id = '".$arr_rule['bs_id']."'"))>0)
			{
				return $this->error_delete.' [id: '.$arr_rule['bs_id']."]<br />";
			}else{
				$this->db->delete('km_barang_supplier', array('bs_id' => $arr_rule['bs_id']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>